<?php

namespace App\B2BProject\Site;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;

class Article extends Model
{
    protected $fillable = ['sort', 'title', 'href', 'display', 'content', 'cover'];

    /**
     * 文章所属的分类
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function category()
    {
        return $this->belongsTo('App\B2BProject\Site\ArticleCategory', 'category_id', 'id');
    }

    /**
     * 文章的创建者
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function author()
    {
        return $this->belongsTo('App\B2BProject\Admin\Administrator', 'admin_id', 'id');
    }

    /**
     * 含有封面的文章集合
     */
    public function scopeCollectionWithCover($query)
    {
        return $query->where('cover', '!=', '');
    }

    /**
     * @param Builder $query
     *
     * @return $this
     */
    public function scopeDisplay(Builder $query)
    {
        return $query->where('display', true);
    }
}

